package cn.idu.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamTest {

    public static void readFile(String path) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(path);
        int n;
        while ((n = fileInputStream.read()) != -1) {
            System.out.println(new String(new byte[]{(byte) n}));
        }
        fileInputStream.close();
    }

    public static void readFileWithTry1(String path) throws IOException {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            int n;
            while ((n = fileInputStream.read()) != -1) {
                System.out.println(new String(new byte[]{(byte) n}));
            }
        } finally {
            if (fileInputStream != null)
                fileInputStream.close();
        }
    }

    public static void readFileWithTry2(String path) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(path)) {
            int n;
            while ((n = fileInputStream.read()) != -1) {
                System.out.println(new String(new byte[]{(byte) n}));
            }
        }
    }

    public static void readFileWithBuffer(String path) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(path)) {
            byte[] buffer = new byte[1024];
            int n;
            while ((n = fileInputStream.read(buffer)) != -1) {
                System.out.println(new String(buffer,0,n));
            }
        }
    }
}
